﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace Gloo.Management {
    /// <summary>
    /// Contains methods and properties for representing a solution.
    /// </summary>
    public class Solution {

        #region Fields

        private string path;
        private string name;
        private List<Project> projects;

        #endregion Fields

        #region Properties

        /// <summary>
        /// Gets or sets the path (folder) of the solution.
        /// </summary>
        public string Path {
            get { return path; }
            set { path = value; }
        }

        /// <summary>
        /// Gets or sets the name of the solution.
        /// </summary>
        public string Name {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// Gets the file name of the solution.
        /// </summary>
        public string FileName
        { get { return string.Format("{0}{1}.gsln", path, name); } }

        /// <summary>
        /// Gets the list of projects in the solution.
        /// </summary>
        public List<Project> Projects {
            get { return projects; }
        }

        #endregion Properties

        #region ctor()

        /// <summary>
        /// Creates a new solution.
        /// </summary>
        /// <param name="name">The name of the solution.</param>
        /// <param name="path">The path of the solution.</param>
        public Solution(string name, string path) {
            this.name = name;
            this.path = path;
            projects = new List<Project>();
            Save();
        }

        #endregion ctor()

        #region Methods

        #region Static

        /// <summary>
        /// Loads a solution from a file.
        /// </summary>
        /// <param name="file">File to load.</param>
        public static Solution FromFile(string file) {
            string name;
            string path;
            List<Project> projects;
            using (XmlTextReader r = new XmlTextReader(file)) {
                // Get properties
                r.Read();
                if (r.Name != "gsln") throw new Exception("Improper solution file.");
                name = r.GetAttribute("name");
                path = r.GetAttribute("path");

                // Get projects
                projects = new List<Project>();
                while (r.Read()) {
                    if (r.Name == "project")
                        projects.Add(Project.FromFile(r.GetAttribute("path")));
                    else if (r.Name != "gsln" && r.Name != "")
                        throw new Exception("Expected 'project' in solution file.");
                }
            }

            Solution sln = new Solution(name, path);
            sln.Projects.AddRange(projects);
            sln.Save();
            return sln;
        }

        #endregion Static

        #region Public

        /// <summary>
        /// Saves the solution.
        /// </summary>
        public void Save() {
            // Easier than fiddling with XmlTextWriter
            using (StreamWriter sw = new StreamWriter(System.IO.File.Create(FileName))) {
                string slnData = @"<gsln name='{0}'
      path='{1}'>
{2}
</gsln>
";
                string projData = "";
                foreach (Project p in projects) {
                    p.Manage(true);
                    projData += string.Format("    <project path='{0}' />" + Environment.NewLine, p.FileName);
                }

                sw.Write(string.Format(slnData, name, path, projData));
            }
        }

        #endregion Public

        #endregion Methods

    }
}